/*******************************************************************
Cipher Backend Work
Tim Foster
November 19, 2008
Sandbox program to work out how to import and substitute cipher text

allow choosing of file name
word wrap at space
add key display, and separate saving (maybe not until gui version?)
this version saves original string and key, rather than two string and solution
*******************************************************************/

using namespace std;

#include <iostream>
#include <fstream>
#include <string>
#include "cipher.h"
#include "solver.h"
#include "dictionary.h"

char defaultCipherFileName[]="cipher.txt";
char defaultKeyFileName[]="key.txt";
//const int maxKeySize=27; //this is the maximum length of the string for the key

void printMenu()
//prints to screen all the menu choices
{
	cout << "(P)rint cipher" <<  endl;
	cout << "(S)ubstitute characters." << endl;
	cout << "Sa(v)e cipher to file." << endl;
	cout << "Engage the (a)utomatic cipher solver!" << endl;
	cout << "(Q)uit program" << endl;
}

void autoSolve(Cipher c1, Key k1)
//attempts to auto-solve the cipher
{
	try{
		Solver solver(c1, k1);
		cout << solver.getSolution() << endl;
	}catch(SolverException e){
		//comment added to see if svn is committing properly
		cout << "Error solving cipher:" << endl;
		cout << e.getMessage() << "\n" << endl;
	}
}

void executeMenuItem(char choice, Cipher &c1, Key &k1)
//runs the associated instructions with each menu item
{
	char char1; //Character substitution values
	char char2;
	
	switch(tolower(choice))
	{
		case 'p':	c1.printCipher(k1);
		            k1.printKey();
					break;
		case 's':	cout << endl << "Which character to replace? ";
			        cin >> char1;
			        cout << endl << "And what to substitute? ";
			        cin >> char2;
			        k1.subChar(char1, char2);
			        c1.printCipher(k1);
		            k1.printKey();
		            break;
		case 'v':   cout << "Attempting save." << endl;
		            c1.saveCipher(defaultCipherFileName);
		            k1.saveKey(defaultKeyFileName);
		            break;
		case 'a':	cout << "Attempting auto-solve." << endl;
					autoSolve(c1, k1);
					break;
		case 'q':   cout << "Bye!" << endl;
		            break;
		default:	cout << "Invalid menu selection, try again." << endl;
	}
}

int main(int argc, char *argv[])
{
	//char fileName[200]; //from which to read cipher

	/*if (argc==2)
	{
		fileName = argv[1];
	}
	else if (argc==3)
	{
		var1 = atoi(argv[1]);
		var2 = atoi(argv[2]);
	}
	else
	{
		cout << "Improper number of arguments input." << endl;
		cout << "arguments are (second optional): sites number_of_electrons_at_which_to_start_calculation" << endl;
		getchar();
	}*/
	Cipher c1; //cipher instance
	Key k1; //key instance

	//attempt to load cipher and key from file
	if (c1.loadCipher(defaultCipherFileName)==1)
	{
		cout<< "Failed to load cipher file.  Quitting." << endl;
		getchar();
		return 1;
	}
	if (k1.loadKey(defaultKeyFileName)==1)
	{
		cout << "Failed to load key file.  Quitting." << endl;
		getchar();
		return 1;
	}
	//initial display of cipher and key
	c1.printCipher(k1);
	k1.printKey();
	
	char char1='p';
	
	while (char1!='q')
	{
		printMenu();
		
		cin >> char1;

		executeMenuItem(char1, c1, k1);
	}
 	getchar();
}
